﻿/**************************************
* Copyright by Spring Studio(http://springstudio.cn)
* 编码添加界面js脚本，及编码解析引擎
***************************************/

/**
* 定义几个全局变量
*/
var serialcreated = false;  // 定义流水号是否通过系统创建（系统创建流水号时锁定编码，手动填入的流水号不锁定）
var infofixed = false;  // 定义编码是否已锁定，在生成流水号时设该值为true，在影响流水号的编码项发生改变时设为false。
var curObj = null;  // 定义当前操作的输入框对象
var curValue = "";  // 定义当前操作对象的当前值（便于在值改变时进行对比）

/**
* 准备好XML对象
*/
var moduleXml = null;
var infocode = null;

if (navigator.userAgent.indexOf("Firefox") > 0) {
    moduleXml = document.implementation.createDocument("text/xml", "", null);
}
else if (navigator.userAgent.indexOf("MSIE") > 0) {
    moduleXml = new ActiveXObject("Microsoft.XMLDOM");
    moduleXml.async = false;
}

try {
    if (document.getElementById("RuleModule").value.trim() == "") {
        throw e;
    }
    else {
        moduleXml.loadXML(document.getElementById("RuleModule").value);
        infocode = moduleXml.getElementsByTagName("infocode")[0];
    }
}
catch (e) {
    alert("系统错误：无法解析编码规则！");
    self.close();
}

var region = null;

/*
* 解析并显示规则表达式
*/
function ParseFormula() {
    var formula = "";
    var regions = infocode.childNodes;
    for (var i = 0; i < regions.length; i++) {
        var opt = "";
        var html = "";
        var size = 1;
        var url = "";
        switch (regions[i].getAttribute("type")) {
            case "singlecate":
                url = "../CodeIndex/DictSelector.aspx?CateGuid=" + regions[i].getAttribute("source");
                if (regions[i].getAttribute("length") != null && regions[i].getAttribute("length") != "") {
                    url += "&length=" + regions[i].getAttribute("length");
                    url += "&supplement=" + regions[i].getAttribute("supplement");
                    url += "&position=" + regions[i].getAttribute("position");
                }
                if (regions[i].getAttribute("level") != null && regions[i].getAttribute("level") != "") {
                    url += "&level=" + regions[i].getAttribute("level");
                }

                opt += regions[i].text + ParseRequest(regions[i].getAttribute("request")) + ParseUnSerial(regions[i].getAttribute("unserial"));
                opt += "<br><iframe src=&quot;" + url + "&quot; style=&quot;width:280px;height:335px;&quot; frameborder=&quot;0&quot; scrolling=&quot;auto&quot;></iframe>";

                if (regions[i].getAttribute("length") != null && regions[i].getAttribute("length") != "") {
                    size = regions[i].getAttribute("length");
                }

                html += "<input type=\"text\" id=\"region_" + i + "\" class=\"region\" regiontype=\"" + regions[i].getAttribute("type") + "\" unserial=\"" + regions[i].getAttribute("unserial") + "\" size=\"" + size + "\" onfocus=\"javascript:RegionOnFocus(this,'" + opt + "');\" onblur=\"javascript:RegionOnBlur(this);\" onkeydown=\"javascript:if(event.keyCode==8){Review(" + i + ");}\" onclick=\"javascript:delay=10;\">";
                break;
            case "info":
                url = "../CodeDesign/InfoSelector.aspx?RuleGuid=" + regions[i].getAttribute("source");
                opt += regions[i].text + ParseRequest(regions[i].getAttribute("request")) + ParseUnSerial(regions[i].getAttribute("unserial"));
                opt += "<br><iframe src=&quot;" + url + "&quot; style=&quot;width:240px;height:335px;&quot; frameborder=&quot;0&quot; scrolling=&quot;auto&quot;></iframe>";
                html += "<input type=\"text\" id=\"region_" + i + "\" class=\"region\" regiontype=\"" + regions[i].getAttribute("type") + "\" unserial=\"" + regions[i].getAttribute("unserial") + "\" size=\"" + size + "\" onfocus=\"javascript:RegionOnFocus(this,'" + opt + "');\" onblur=\"javascript:RegionOnBlur(this);\" onkeydown=\"javascript:if(event.keyCode==8){Review(" + i + ");}\" onclick=\"javascript:delay=10;\">";
                break;
            case "infoserial":
                url = "../CodeDesign/ExistSerialSelector.aspx?RuleGuid=" + regions[i].getAttribute("source");
                opt += regions[i].text + ParseRequest(regions[i].getAttribute("request")) + ParseUnSerial(regions[i].getAttribute("unserial"));
                opt += "<br><iframe src=&quot;" + url + "&quot; style=&quot;width:180px;height:240px;&quot; frameborder=&quot;0&quot; scrolling=&quot;auto&quot;></iframe>";
                html += "<input type=\"text\" id=\"region_" + i + "\" class=\"region\" regiontype=\"" + regions[i].getAttribute("type") + "\" unserial=\"" + regions[i].getAttribute("unserial") + "\" size=\"" + size + "\" onfocus=\"javascript:RegionOnFocus(this,'" + opt + "');\" onblur=\"javascript:RegionOnBlur(this);\" onkeydown=\"javascript:if(event.keyCode==8){Review(" + i + ");}\" onclick=\"javascript:delay=10;\">";
                break;
            case "year":
                opt += regions[i].text + ParseRequest(regions[i].getAttribute("request")) + ParseUnSerial(regions[i].getAttribute("unserial"));

                if (regions[i].getAttribute("current") == "1") {
                    opt += "<br>[<a style=&quot;color:#000080;cursor:pointer;&quot; onclick=javascript:SetObjValue(&quot;" + new Date().getFullYear() + "&quot;);>必须是当前年份</a>]";
                }
                else {
                    for (y = new Date().getFullYear(); y > new Date().getFullYear() - 10; y--) {
                        if (y == new Date().getFullYear()) {
                            opt += "<br>[<a style=&quot;color:#ff0000;cursor:pointer;&quot; onclick=javascript:SetObjValue(&quot;" + y + "&quot;);>" + y + "</a>]";
                        }
                        else {
                            opt += "<br>[<a style=&quot;color:#000080;cursor:pointer;&quot; onclick=javascript:SetObjValue(&quot;" + y + "&quot;);>" + y + "</a>]";
                        }
                    }
                }

                size = 4;

                html += "<input type=\"text\" id=\"region_" + i + "\" class=\"region\" regiontype=\"" + regions[i].getAttribute("type") + "\" unserial=\"" + regions[i].getAttribute("unserial") + "\" value=\"" + new Date().getFullYear() + "\" size=\"" + size + "\" onfocus=\"javascript:RegionOnFocus(this,'" + opt + "');\" onblur=\"javascript:RegionOnBlur(this);\" onkeydown=\"javascript:if(event.keyCode==8){Review(" + i + ");}\" onclick=\"javascript:delay=10;\">";
                break;
            case "month":
                opt += regions[i].text + ParseRequest(regions[i].getAttribute("request")) + ParseUnSerial(regions[i].getAttribute("unserial"));

                var curMonth = new Date().getMonth() + 1 + "";
                if (curMonth.length == 1) {
                    curMonth = "0" + curMonth;
                }

                if (regions[i].getAttribute("current") == "1") {
                    opt += "<br>[<a style=&quot;color:#000080;cursor:pointer;&quot; onclick=javascript:SetObjValue(&quot;" + curMonth + "&quot;);>必须是当前月份</a>]";
                }
                else {
                    for (m = 1; m <= 12; m++) {
                        var mstr = m + "";
                        if (mstr.length == 1) {
                            mstr = "0" + mstr;
                        }
                        
                        if (mstr == curMonth) {
                            opt += "<br>[<a style=&quot;color:#ff0000;cursor:pointer;&quot; onclick=javascript:SetObjValue(&quot;" + mstr + "&quot;);>" + mstr + "</a>]";
                        }
                        else {
                            opt += "<br>[<a style=&quot;color:#000080;cursor:pointer;&quot; onclick=javascript:SetObjValue(&quot;" + mstr + "&quot;);>" + mstr + "</a>]";
                        }
                    }
                }

                size = 2;

                html += "<input type=\"text\" id=\"region_" + i + "\" class=\"region\" regiontype=\"" + regions[i].getAttribute("type") + "\" unserial=\"" + regions[i].getAttribute("unserial") + "\" value=\"" + curMonth + "\" size=\"" + size + "\" onfocus=\"javascript:RegionOnFocus(this,'" + opt + "');\" onblur=\"javascript:RegionOnBlur(this);\" onkeydown=\"javascript:if(event.keyCode==8){Review(" + i + ");}\" onclick=\"javascript:delay=10;\">";
                break;
            case "day":
                opt += regions[i].text + ParseRequest(regions[i].getAttribute("request")) + ParseUnSerial(regions[i].getAttribute("unserial"));

                var curDay = new Date().getDate() + "";
                if (curDay.length == 1) {
                    curDay = "0" + curDay;
                }

                if (regions[i].getAttribute("current") == "1") {
                    opt += "<br>[<a style=&quot;color:#000080;cursor:pointer;&quot; onclick=javascript:SetObjValue(&quot;" + curDay + "&quot;);>必须是当前日期</a>]";
                }
                else {
                    for (d = 1; d <= 31; d++) {
                        var dstr = d + "";
                        if (dstr.length == 1) {
                            dstr = "0" + dstr;
                        }

                        if ((d - 1) % 10 == 0) {
                            opt += "<br>";
                        }

                        if (dstr == curDay) {
                            opt += "[<a style=&quot;color:#ff0000;cursor:pointer;&quot; onclick=javascript:SetObjValue(&quot;" + dstr + "&quot;);>" + dstr + "</a>]";
                        }
                        else {
                            opt += "[<a style=&quot;color:#000080;cursor:pointer;&quot; onclick=javascript:SetObjValue(&quot;" + dstr + "&quot;);>" + dstr + "</a>]";
                        }
                    }
                }

                size = 2;

                html += "<input type=\"text\" id=\"region_" + i + "\" class=\"region\" regiontype=\"" + regions[i].getAttribute("type") + "\" unserial=\"" + regions[i].getAttribute("unserial") + "\" value=\"" + curDay + "\" size=\"" + size + "\" onfocus=\"javascript:RegionOnFocus(this,'" + opt + "');\" onblur=\"javascript:RegionOnBlur(this);\" onkeydown=\"javascript:if(event.keyCode==8){Review(" + i + ");}\" onclick=\"javascript:delay=10;\">";
                break;
            case "text":
                if (regions[i].getAttribute("source") == "preset") {
                    html += regions[i].getAttribute("text");
                }
                else if (regions[i].getAttribute("source") == "manual") {
                    opt += regions[i].text + ParseRequest(regions[i].getAttribute("request")) + ParseUnSerial(regions[i].getAttribute("unserial"));
                    
                    if (regions[i].getAttribute("length") != "0") {
                        size = regions[i].getAttribute("length");
                    }

                    if (regions[i].getAttribute("default") != "") {
                        opt += "<br>默认值：<a style=&quot;color:#000080;cursor:pointer;&quot; onclick=javascript:SetObjValue(&quot;" + regions[i].getAttribute("default") + "&quot;);>" + regions[i].getAttribute("default") + "</a>";
                        size = regions[i].getAttribute("default").length;
                        html += "<input type=\"text\" id=\"region_" + i + "\" class=\"region\" regiontype=\"" + regions[i].getAttribute("type") + "\" unserial=\"" + regions[i].getAttribute("unserial") + "\" size=\"" + size + "\" value=\"" + regions[i].getAttribute("default") + "\" onfocus=\"javascript:RegionOnFocus(this,'" + opt + "');\" onblur=\"javascript:RegionOnBlur(this);\" onkeydown=\"javascript:if(event.keyCode==8){Review(" + i + ");}\" onclick=\"javascript:delay=10;\">";
                    }
                    else {
                        html += "<input type=\"text\" id=\"region_" + i + "\" class=\"region\" regiontype=\"" + regions[i].getAttribute("type") + "\" unserial=\"" + regions[i].getAttribute("unserial") + "\" size=\"" + size + "\" onfocus=\"javascript:RegionOnFocus(this,'" + opt + "');\" onblur=\"javascript:RegionOnBlur(this);\" onkeydown=\"javascript:if(event.keyCode==8){Review(" + i + ");}\" onclick=\"javascript:delay=10;\">";
                    }
                }
                break;
            case "serial":
                opt += regions[i].text + ParseRequest(regions[i].getAttribute("request")) + ParseUnSerial(regions[i].getAttribute("unserial"));
                if (regions[i].getAttribute("exist") != null && regions[i].getAttribute("exist") == "1") {
                    url = "../CodeDesign/ExistSerialSelector.aspx?RuleGuid=" + document.getElementById("RuleGuid").value;
                    opt += "<br><iframe src=&quot;" + url + "&quot; style=&quot;width:180px;height:240px;&quot; frameborder=&quot;0&quot; scrolling=&quot;auto&quot;></iframe>";
                }
                opt += "<center><input type=&quot;button&quot; value=&quot;新生成&quot; style=&quot;cursor:pointer;&quot; onclick=&quot;javascript:CreateSerial();&quot;>";

                if (regions[i].getAttribute("length") != "0") {
                    opt += "<font color=#808080>[" + regions[i].getAttribute("length") + "位流水号]</font></center>";
                }
                else {
                    opt += "<font color=#808080>[普通流水号]</font></center>";
                }

                if (regions[i].getAttribute("length") != "0") {
                    size = regions[i].getAttribute("length");
                }

                html += "<input type=\"text\" id=\"region_" + i + "\" class=\"region\" regiontype=\"" + regions[i].getAttribute("type") + "\" unserial=\"" + regions[i].getAttribute("unserial") + "\" size=\"" + size + "\" onfocus=\"javascript:RegionOnFocus(this,'" + opt + "');\" onblur=\"javascript:RegionOnBlur(this);\" onkeydown=\"javascript:if(event.keyCode==8){Review(" + i + ");}\" onclick=\"javascript:delay=10;\">";
                break;
            case "multicate":
                opt += regions[i].text + ParseRequest(regions[i].getAttribute("request")) + ParseUnSerial(regions[i].getAttribute("unserial")) +"<br>";
                var arrSource = regions[i].getAttribute("source").split(',');
                var width = 360 / arrSource.length;
                for (x = 0; x < arrSource.length; x++) {
                    opt += "<iframe id=&quot;dict_" + x + "&quot; src=&quot;../CodeIndex/DictSelector.aspx?CateGuid=" + arrSource[x] + "&multiselect=true&separator=" + regions[i].getAttribute("separator") + "&quot; style=&quot;width:" + width + "px;height:335px;&quot; frameborder=&quot;0&quot; scrolling=&quot;auto&quot;></iframe>";
                }

                opt += "<center><input type=&quot;button&quot; value=&quot;确定&quot; style=&quot;cursor:pointer;&quot; onclick=javascript:SetObjValue(GetMultiValue(" + arrSource.length + ",&quot;" + regions[i].getAttribute("separator") + "&quot;));><br><span style=&quot;color:#ff0000;font-weight:bold;&quot;>提示：</span>请依次选择各索引项的值，然后点击“确定”按钮</center>";
                html += "<input type=\"text\" id=\"region_" + i + "\" class=\"region\" regiontype=\"" + regions[i].getAttribute("type") + "\" unserial=\"" + regions[i].getAttribute("unserial") + "\" size=\"" + size + "\" onfocus=\"javascript:RegionOnFocus(this,'" + opt + "');\" onblur=\"javascript:RegionOnBlur(this);\" onkeydown=\"javascript:if(event.keyCode==8){Review(" + i + ");}\" onclick=\"javascript:delay=10;\">";
                break;
        }

        formula += " " + html + " ";
    }

    document.getElementById("formula").innerHTML = formula;
}

ParseFormula();  // 页面载入时显示表达式

function SetObjValue(str) {

    curObj.value = str;

    if (curObj.value.length > 0) {
        curObj.value = curObj.value.toUpperCase();
        curObj.size = curObj.value.length;
    }

    if (curObj.regiontype != "serial" && curObj.unserial != "1" && curObj.value != curValue) {
        infofixed = false;
    }

    Next(curObj.id.replace("region_", ""));
    HiddenOptions();
}

/*
* 解析必填项与选填项
*/
function ParseRequest(request) {
    if (request == "optional") {
        return "<font color=#808080>[选填项]</font>";
    }
    else if (request == "needed") {
        return "<font color=#808080>[必填项]</font>";
    }
    else {
        return "";
    }
}

/*
* 解析是否影响生成流水号
*/
function ParseUnSerial(unserial) {
    if (unserial != null && unserial == "1") {
        return "<font color=#808080>[不影响生成流水号]</a>";
    }
    else {
        return "";
    }
}

/*
* 按退格键删除文本框内容时，光标自动前移到上一个输入框
*/
function Review(n) {
    if (document.getElementById("region_" + n).value == "") {
        for (var i = n - 1; i >= 0; i--) {
            var obj = document.getElementById("region_" + i);
            if (obj != null && obj != undefined) {
                obj.focus();
                var oRange = document.selection.createRange();
                oRange.move("character", obj.value.length);
                oRange.select();
                return;
            }
        }
    }
}

/*
* 用选择的方式赋给文本框内容时，光标自动后移到下一个输入框
*/
function Next(n) {
    var count = infocode.childNodes.length;
    for (var i = n - 1 + 2; i <= count; i++) {
        var obj = document.getElementById("region_" + i);
        if (obj != null && obj != undefined) {
            obj.focus();
            obj.select();
            return;
        }
    }
}

/*
* 输入框获得焦点时，初始化当前操作项、当前值，并自动弹出提示框
*/
function RegionOnFocus(obj, opt) {
    curObj = obj;
    curValue = curObj.value;
    ShowOptions(obj, opt);
}

/*
* 输入框失去焦点时，转换字母为大写，并自动伸展输入框；并判断影响生成流水号的项发生改变时，解锁编码
*/
function RegionOnBlur(obj) {
    if (obj.value.length > 0) {
        obj.value = obj.value.toUpperCase();
        obj.size = obj.value.length;
    }

    if (curObj.regiontype != "serial" && obj.unserial != "1" && obj.value != curValue) {
        infofixed = false;
    }
}

/*
* 提示兼操作弹出框
*/
document.write("<div id=\"options\" onclick=\"javascript:delay=10;\" onmousemove=\"javascript:delay=10;\" style=\"text-align:left; padding:5px 5px 5px 5px; overflow: auto; position: absolute; z-index: 100; left: 100px; top: 100px; visibility: hidden; border: 1px solid #82AAD4; background-color: #ffffec; FILTER: progid:DXImageTransform.Microsoft.dropshadow(OffX=2, OffY=2, Color=#C0C0FF, Positive=true)\"></div>");

var delay = 10;
function SetDelay() {
    if (delay > 0) {
        delay = delay - 1;
    }
    setTimeout(SetDelay, 200);
}
SetDelay();

function ShowOptions(obj, opt) {
    delay = 10;
    var options = document.getElementById("options");
    options.style.left = "0";
    options.style.top = "0";
    options.innerHTML = opt;
    if (options.offsetWidth + getLeft(obj) < document.body.offsetWidth) {
        options.style.left = getLeft(obj);
    }
    else {
        options.style.left = getLeft(obj) + obj.offsetWidth - options.offsetWidth + 2;
    }
    options.style.top = getTop(obj) + obj.offsetHeight;
    options.style.visibility = "visible";
}

function getTop(e) {
    var offset = e.offsetTop;
    if (e.offsetParent != null) offset += getTop(e.offsetParent);
    return offset;
}

function getLeft(e) {
    var offset = e.offsetLeft;
    if (e.offsetParent != null) offset += getLeft(e.offsetParent);
    return offset;
}

function HiddenOptions() {
    var options = document.getElementById("options");
    options.innerHTML = "";
    options.style.visibility = "hidden";
}

function AutoHidden() {
    if (delay <= 8) {
        HiddenOptions();
    }
}
document.attachEvent('onclick', AutoHidden); // 页面全局事件处理，增加document.onclick事件处理

/*
* 获取多项索引字典值返回的值
*/
function GetMultiValue(multi_length, separator) {
    var rtn = "";
    for (i = 0; i < multi_length; i++){
        if (document.frames["dict_" + i].GetReturnDesc() != "") {
            rtn += document.frames["dict_" + i].GetReturnDesc().split('-')[0] + separator;
        }
    }
    rtn = rtn.substring(0, rtn.length - separator.length);

    return rtn;
}

/*
* 检查已输入项的合法性（对于留空尚未输入的项，暂不检查）
*/
function CheckValidate() {
    var regions = infocode.childNodes;

    for (var i = 0; i < regions.length; i++) {
        var obj = null;

        // 检查已输入项的内容中是否含有单引号或双引号
        switch (regions[i].getAttribute("type")) {
            case "singlecate":
            case "info":
            case "infoserial":
            case "year":
            case "month":
            case "day":
            case "multicate":
                obj = document.getElementById("region_" + i);
                if (obj != null && obj != undefined) {
                    if (checkQuot(obj.value)) {
                        alert("“" + regions[i].text + "”中不能含有单引号或双引号！");
                        obj.focus();
                        obj.select();
                        return false;
                    }
                }
                break;
            case "text":
                if (regions[i].getAttribute("source") == "manual") {
                    obj = document.getElementById("region_" + i);
                    if (obj != null && obj != undefined) {
                        if (checkQuot(obj.value)) {
                            alert("“" + regions[i].text + "”中不能含有单引号或双引号！");
                            obj.focus();
                            obj.select();
                            return false;
                        }
                    }
                }
                break;
        }

        // 如规定了长度限制，检查已输入项的内容是否符合长度定义
        if (regions[i].getAttribute("type") != "serial" && regions[i].getAttribute("length") != null && regions[i].getAttribute("length") != "0") {
            obj = document.getElementById("region_" + i);
            if (obj != null && obj != undefined) {
                obj.value = obj.value.trim();
                if (obj.value != "" && obj.value.length != regions[i].getAttribute("length")) {
                    alert("“" + regions[i].text + "”长度限定为" + regions[i].getAttribute("length") + "！");
                    obj.focus();
                    obj.select();
                    return false;
                }
            }
        }

        // 检查已输入项的年份是否合法；如有当前年份限制的，检查是否符合当前年份限制
        if (regions[i].getAttribute("type") == "year") {
            obj = document.getElementById("region_" + i);
            if (obj != null && obj != undefined) {
                obj.value = obj.value.trim();
                if (obj.value != "") {
                    if (!checkNaturalNumber(obj.value) || obj.value < 1000 || obj.value > 3000) {
                        alert("“" + regions[i].text + "”的值不合法！");
                        obj.focus();
                        obj.select();
                        return false;
                    }

                    // 检查是否符合当前年份限制
                    if (regions[i].getAttribute("current") == "1" && obj.value != new Date().getFullYear()) {
                        alert("“" + regions[i].text + "”必须为当前年份！");
                        obj.focus();
                        obj.select();
                        return false;
                    }
                }
            }
        }

        // 检查已输入项的月份是否合法；如有当前月份限制的，检查是否符合当前月份限制
        if (regions[i].getAttribute("type") == "month") {
            obj = document.getElementById("region_" + i);
            if (obj != null && obj != undefined) {
                obj.value = obj.value.trim();
                if (obj.value != "") {
                    if (!checkNaturalNumber(obj.value) || obj.value < 1 || obj.value > 12) {
                        alert("“" + regions[i].text + "”的值不合法！");
                        obj.focus();
                        obj.select();
                        return false;
                    }

                    // 检查是否符合当前月份限制
                    if (regions[i].getAttribute("current") == "1" && parseInt(obj.value) != new Date().getMonth() + 1) {
                        alert("“" + regions[i].text + "”必须为当前月份！");
                        obj.focus();
                        obj.select();
                        return false;
                    }
                }
            }
        }

        // 检查已输入项的日期是否合法；如有当前日期限制的，检查是否符合当前日期限制
        if (regions[i].getAttribute("type") == "day") {
            obj = document.getElementById("region_" + i);
            if (obj != null && obj != undefined) {
                obj.value = obj.value.trim();
                if (obj.value != "") {
                    if (!checkNaturalNumber(obj.value) || obj.value < 1 || obj.value > 31) {
                        alert("“" + regions[i].text + "”的值不合法！");
                        obj.focus();
                        obj.select();
                        return false;
                    }

                    // 检查是否符合当前月份限制
                    if (regions[i].getAttribute("current") == "1" && parseInt(obj.value) != new Date().getDate()) {
                        alert("“" + regions[i].text + "”必须为当前日期！");
                        obj.focus();
                        obj.select();
                        return false;
                    }
                }
            }
        }
    }

    return true;
}

/*
* 检查除流水号之外的其他输入项，利用Ajax技术获取规定的流水号（传递流水号参考码、流水号长度、流水号起始编号三个参数）
*/
function CreateSerial() {

    var reference = "";
    var seriallength = "";
    var serialstart = "";

    var regions = infocode.childNodes;
    var obj = null;

    // 检查是必填项且影响流水号的项是否为空
    for (i = 0; i < regions.length; i++) {
        if (regions[i].getAttribute("type") != "serial" && regions[i].getAttribute("request") == "needed") {
            if (regions[i].getAttribute("unserial") == null || regions[i].getAttribute("unserial") != "1") {
                obj = document.getElementById("region_" + i);
                if (obj != null && obj != undefined) {
                    obj.value = obj.value.trim();
                    if (obj.value == "") {
                        alert("“" + regions[i].text + "”不能为空！");
                        obj.focus();
                        return false;
                    }
                }
            }
        }
    }

    // 检查已输入项的合法性
    if (!CheckValidate()) {
        return false;
    }

    // 获取流水号参考码
    for (i = 0; i < regions.length; i++) {
        switch (regions[i].getAttribute("type")) {
            case "singlecate":
            case "info":
            case "infoserial":
            case "year":
            case "month":
            case "day":
            case "multicate":
                if (regions[i].getAttribute("unserial") == null || regions[i].getAttribute("unserial") != "1") {
                    obj = document.getElementById("region_" + i);
                    if (obj != null && obj != undefined) {
                        obj.value = obj.value.trim();
                        reference += obj.value;
                    }
                }
                break;
            case "text":
                if (regions[i].getAttribute("source") == "preset") {
                    reference += regions[i].getAttribute("text");
                }
                else if (regions[i].getAttribute("source") == "manual") {
                    if (regions[i].getAttribute("unserial") == null || regions[i].getAttribute("unserial") != "1") {
                        obj = document.getElementById("region_" + i);
                        if (obj != null && obj != undefined) {
                            obj.value = obj.value.trim();
                            reference += obj.value;
                        }
                    }
                }
                break;
            case "serial":
                reference += "{serial}";
                seriallength = regions[i].getAttribute("length");
                serialstart = regions[i].getAttribute("start");
                break;
        }
    }

    // 获取流水号
    GetSerial(reference, seriallength, serialstart);
    return true;
}

/*
* 调用XMLHTTP获取流水号
*/
function GetSerial(reference, seriallength, serialstart) {
    var xmlhttp = GetXmlHttp();
    if (xmlhttp != null) {
        // 定义回调函数
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4) {
                if (xmlhttp.status == 200) {
                    if (xmlhttp.responseText == "error") {
                        alert("流水号生成过程中出现错误，请与系统管理员联系！");
                    }
                    else {
                        curObj.value = xmlhttp.responseText;
                        if (curObj.value.length > 0) {
                            curObj.size = curObj.value.length;
                        }
                        serialcreated = true;  // 设置流水号已通过程序创建
                        infofixed = true;  // 设置编码已锁定
                    }
                    HiddenOptions();
                }
            }
        }
        
        var url = "../CodeDesign/GetSerial.aspx?RuleGuid=" + document.getElementById("RuleGuid").value + "&SerialReference=" + reference + "&SerialLength=" + seriallength + "&SerialStart=" + serialstart + "&Random=" + Math.random();
        xmlhttp.open("GET", url, true);
        xmlhttp.send(null);
    }
}

/*
* 检查各输入项，并获取最终生成的信息编码
*/
function GetInfo() {
    var info = "";
    var reference = "";
    var number = "";
    
    var regions = infocode.childNodes;
    for (i = 0; i < regions.length; i++) {
        var obj = null;
        
        // 检查必填项是否为空
        if (regions[i].getAttribute("request") == "needed") {
            obj = document.getElementById("region_" + i);
            if (obj != null && obj != undefined) {
                obj.value = obj.value.trim();
                if (obj.value == "") {
                    alert("“" + regions[i].text + "”是必填项，不能为空！");
                    obj.focus();
                    return false;
                }
            }
        }
    }

    // 检查已输入项的合法性
    if (!CheckValidate()) {
        return false;
    }

    // 获取流水号参考码
    for (i = 0; i < regions.length; i++) {
        
        // 检查流水号是否合法
        if (regions[i].getAttribute("type") == "serial") {
            obj = document.getElementById("region_" + i);
            if (obj != null && obj != undefined) {
                obj.value = obj.value.trim();
                if (!checkNaturalNumber(obj.value)) {
                    alert("“" + regions[i].text + "”必须为正整数！");
                    obj.focus();
                    obj.select();
                    return false;
                }

                if (regions[i].getAttribute("length") != null && regions[i].getAttribute("length") != "0" && obj.value.length != regions[i].getAttribute("length")) {
                    alert("“" + regions[i].text + "”的长度不符合定义！");
                    obj.focus();
                    obj.select();
                    return false;
                }

                if (regions[i].getAttribute("start") != null && regions[i].getAttribute("start") != "" && obj.value < regions[i].getAttribute("start")) {
                    alert("“" + regions[i].text + "”必须大于初始值“" + regions[i].getAttribute("start") + "”！");
                    obj.focus();
                    obj.select();
                    return false;
                }
            }
        }

        // 获取最终编码
        switch (regions[i].getAttribute("type")) {
            case "singlecate":
            case "info":
            case "infoserial":
            case "year":
            case "month":
            case "day":
            case "multicate":
                obj = document.getElementById("region_" + i);
                if (obj != null && obj != undefined) {
                    obj.value = obj.value.trim();
                    info += obj.value;  // 最终编码
                }
                
                if (regions[i].getAttribute("unserial") == null || regions[i].getAttribute("unserial") != "1") {
                    obj = document.getElementById("region_" + i);
                    if (obj != null && obj != undefined) {
                        obj.value = obj.value.trim();
                        reference += obj.value;  // 流水号参考码
                    }
                }
                break;
            case "text":
                if (regions[i].getAttribute("source") == "preset") {
                    info += regions[i].getAttribute("text");
                }
                else if (regions[i].getAttribute("source") == "manual") {
                    obj = document.getElementById("region_" + i);
                    if (obj != null && obj != undefined) {
                        obj.value = obj.value.trim();
                        info += obj.value;  // 最终编码
                    }
                }
                
                if (regions[i].getAttribute("source") == "preset") {
                    reference += regions[i].getAttribute("text");
                }
                else if (regions[i].getAttribute("source") == "manual") {
                    if (regions[i].getAttribute("unserial") == null || regions[i].getAttribute("unserial") != "1") {
                        obj = document.getElementById("region_" + i);
                        if (obj != null && obj != undefined) {
                            obj.value = obj.value.trim();
                            reference += obj.value;  // 流水号参考码
                        }
                    }
                }
                break;
            case "serial":
                obj = document.getElementById("region_" + i);
                if (obj != null && obj != undefined) {
                    obj.value = obj.value.trim();
                    info += obj.value;  // 最终编码
                    reference += "{serial}";  // 流水号参考码
                    number = obj.value;  // 当前流水号值
                }
                break;
        }
    }

    // 判断编码是否已锁定（即流水号生成后其他影响流水号的编码项的值没有改变）
    if (serialcreated == true && infofixed == false) {
        alert("影响流水号的编码项的值已经改变，请重新生成流水号！");
        return false;
    }

    // 检查编码说明中是否包含单引号或双引号
    if (checkQuot(document.getElementById("InfoDesc").value)) {
        alert("编码说明中不能含单引号或双引号！");
        document.getElementById("InfoDesc").focus();
        return false;
    }

    document.getElementById("InfoCode").value = info;
    document.getElementById("SerialReference").value = reference;
    document.getElementById("SerialNumber").value = number;

    return true;  // 至此，创建新的编码所需的各个参数均已获取
}